package com.jiajunsong.blog.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Service;

import com.jiajunsong.blog.domain.User;
import com.jiajunsong.blog.repository.UserRepository;

@Service
public class UserDetailsServiceImpl implements UserDetailsService, SocialUserDetailsService {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private UserRepository userRepository;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		return userRepository.findByUsername(username);
	}

	@Override
	public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
		logger.info("社交登录用户Id:" + userId);
		// 根据用户名查找用户信息
		// 根据查找到的用户信息判断用户是否被冻结

		User user = userRepository.findByNickname(userId);
		if (user != null) {
			return new SocialUser(user.getUsername(), user.getPassword(), user.isEnabled(), user.isAccountNonExpired(),
					user.isCredentialsNonExpired(), user.isAccountNonLocked(), user.getAuthorities());
		} else {
			return null;
			/*return new SocialUser(userId, "", true, true,
					true, true, AuthorityUtils.NO_AUTHORITIES);*/
		}

	}

}
